package io.github.xxyopen.novel.service.impl;

import io.github.xxyopen.novel.dao.entity.Participle;
import io.github.xxyopen.novel.dao.mapper.ParticipleMapper;
import io.github.xxyopen.novel.service.ParticipleService;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ParticipleServiceImpl implements ParticipleService {
    @Autowired
    ParticipleMapper participleMapper;

    @Override
    public Boolean saveParticiple(String word) {
        return null;
    }

    @Override
    public Boolean batchSaveParticiple(List<String> words) {
        List<Participle> participles = new ArrayList<>();
        List<String> existWords = getExistWords(words);
        words.removeAll(existWords);
        for (String word:words) {
            Participle participle = new Participle();
            participle.setWord(word);
            participles.add(participle);
        }
        if (participles.size()>0) {
            participleMapper.batchSaveParticiple(participles);
        }
        return true;
    }

    @Override
    public Boolean deleteByWord(String word) {
        participleMapper.deleteParticiple(word);
        return true;
    }

    @Override
    public Boolean deleteById(Long id) {
        participleMapper.deleteById(id);
        return true;
    }

    @Override
    public List<String> getExistWords(List<String> words) {
       return participleMapper.getExistWords(words);
    }
}
